﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using MySql.Data.MySqlClient;

namespace ETALON
{
    enum modeWhatCutPaste { oneNode_object, oneNude_folder, moreNode_object, moreNode_folder, oneNode_OF, moreNode_OF }
    enum modeMove { noMove,
        select_sourse_InTreeView, 
        clickOnFlag_ListView_enableFalse,
        select_adress_inTreeView_ready_move}
    enum modeCut { selectSourse, 
        selectFolder_or_Object_and_enabled_ButtonCut, 
        CUT_only_on_ListView, 
        selectFolder_adress_ready_to_Paste,
        PASTE,
        readyToSelectSourse}


    public partial class FormTREELV : Form
    {

        #region declaration
        
        DataTable df;  bool IsSelectNode;
        functionService.prm_city q; 
        //fs.prm_city q;
        //at = fs.shorts.a(p[i][1], ";", p[i][2], ";", p[i][5]);
        const string LEV_1 = "lev", COD_2 = "cod", FO_3 = "fo";
        const int iLEV_1 = 0, iCOD_2 = 1, iFO_3 = 2;
        const int df_fields_lev_1=1, df_fields_cod_2=2, df_fields_name_3=3, df_fields_cop_4=4, df_fields_fo_5=5;  



        string currentNode_tag;

        string cutNode_tag, pasteNode_tag; bool IsSelect_ForCut, IsCut;

        modeWhatCutPaste current_mocp;

        string cutNodeParent_tag;
       // Hashtable ht_currentNode;

        //--------------------------------
       // TreeNode nodeCurrent, nodeCut, nodeCurrentParent_forCut;
        TreeNode nodeSelect_as_sourse, nodeSelect_for_cut, nodeSelect_as_adress_for_Paste;
        TreeNode[] a_nodeSelect_for_cut;
        //--------------------------------

       // modeMove currencyModeMove; 
        modeCut currentModeCut;
        int selectSourseNodeIndex;


        string TABLENAME, SCHEMA;


        #endregion

        public FormTREELV()
        {
            InitializeComponent(); 
        }
        public FormTREELV(string schemaName, string tableName): this()
        {
            InitializeComponentMy(schemaName,tableName);
        }

        #region ini
        void InitializeComponentMy(string schemaName, string tableName)
        {
            SCHEMA = schemaName;
            TABLENAME = tableName;
            //q= new SERVICE(); q.co();
            q = new functionService.prm_city(); q.con.pass =  q.tk(param.pass_inServ_55); q.con.co(q.tk(param.server_1));

            df = q.selectAny(functionService.shorts.a("call `city`.`select_treea`('", SCHEMA, "', '", TABLENAME, "');")); 
            
            //q.sh(df);
            
            
            df_load();
            IsSelectNode = false;
            currentNode_tag = "0;0;0"; 
            IsCut = false; current_mocp = modeWhatCutPaste.moreNode_object;
            setModeMove(modeMove.noMove );
            selectSourseNodeIndex = 0;
            this.Text = "Список";//q.tc(param.default_3);
            //pr.put((int)param.default_3, "Стол"); 
        }

        #endregion

        #region work fun

        void afterExp()
        {
            if (IsSelectNode)
            {

                // selectSourseNodeIndex = treeView1.SelectedNode.Index;fs.shorts.m(selectSourseNodeIndex);

                setModeCut(modeCut.selectSourse);
                string ag;
                int indexsel = treeView1.SelectedNode.Index;
                string st = treeView1.SelectedNode.Text;

                ag = treeView1.SelectedNode.Tag.ToString();
                int lev = Convert.ToInt32(readTAG(ag, iLEV_1));
                object oc = readTAG(ag, iCOD_2);

                #region LOAD---

                Level_any_readTableParent_readTableChild
                    (
                    treeView1.SelectedNode, indexsel, oc,
                    lev
                    );



                #endregion

                IsSelectNode = false;
            }
        }




        private void Level_any_readTableParent_readTableChild(TreeNode tno,
         int indexSelectNode, object objectCodParent, int levo)
        {
            DataView p = new DataView(df);
            p.RowFilter = string.Concat("cop = ", objectCodParent, " and lev =", levo + 1);

            int ip = p.Count; //fs.shorts.m(fs.shorts.aa(ip, "=ip"));

            //------------------------------------------------------folder cut
            if (ip > 0) setModeCut(modeCut.selectSourse);
            //------------------------------------------------------

            DataView c = new DataView(df); //c.RowFilter = string.Concat("cop = ", objectCodParent, " and lev =", levo);
            TreeNode nn, nnn;
            string ag = "";
            //Hashtable hta = new Hashtable(); setHTA(hta, 0,0,0);

            tno.Nodes.Clear();
            for (int i = 0; i < ip; i++)
            {
                //  setHTA(hta, p[i][iLEV_1], p[i][iCOD_2], p[i][iFO_3]);


                nn = new TreeNode(functionService.nameCod.name_cod(p[i][3], p[i][2]));
                c.RowFilter = string.Concat("cop = ", p[i][2], " and lev = ", levo + 2);

                for (int j = 0; j < c.Count; j++)
                {
                    //setHTA(hta, c[j][iLEV_1], c[j][iCOD_2], c[j][iFO_3]);
                    ag = functionService.shorts.a(c[j][1], ";", c[j][2], ";", c[j][5]);
                    nnn = new TreeNode(functionService.nameCod.name_cod(c[j][3], c[j][2])); nnn.Tag = ag; nnn.ImageIndex = imageIndex(c[j][5], true); nnn.SelectedImageIndex = imageIndex(c[j][5], false);
                    nn.Nodes.Add(nnn);
                }
                ag = functionService.shorts.a(p[i][1], ";", p[i][2], ";", p[i][5]); nn.Tag = ag; nn.ImageIndex = imageIndex(p[i][5], true); nn.SelectedImageIndex = imageIndex(p[i][5], false);
                tno.Nodes.Add(nn);
            }


            readTableChild_listView(listView1, indexSelectNode, objectCodParent, levo);


        }

        private int imageIndex(object pi5, bool true_no_select)
        {
            int ir = 0;
            int[] aObj = new int[] { 10, 11 }; int[] aFld = new int[] { 8, 9 };
            if (pi5.ToString() == "1")
            {
                if (true_no_select) ir = aObj[0]; else ir = aObj[1];
            }
            else
            {
                if (true_no_select) ir = aFld[0]; else ir = aFld[1];
            }
            return ir;
        }
        private int imageIndexLV(object pi5, bool true_no_select)
        {
            int ir = 0;
            int[] aObj = new int[] { 0, 1 }; int[] aFld = new int[] { 0, 1 };
            if (pi5.ToString() == "1")
            {
                if (true_no_select) ir = aObj[0]; else ir = aObj[1];
            }
            else
            {
                if (true_no_select) ir = aFld[0]; else ir = aFld[1];
            }
            return ir;
        }

        private void readTableChild_listView(ListView lv,
         int indexSelectNode, object objectCodParent, int levo)
        {
            DataView p = new DataView(df);
            p.RowFilter = string.Concat("cop = ", objectCodParent, " and lev =", levo + 1);

            int ip = p.Count;
            DataView c = new DataView(df);
            string ag = "";

            string[] sa = new string[ip];

            lv.Items.Clear();
            for (int i = 0; i < ip; i++)
            {
                lv.Items.Add(functionService.nameCod.name_cod(p[i][df_fields_name_3], p[i][df_fields_cod_2]),
                    imageIndex(p[i][df_fields_fo_5], true));
                ag = functionService.shorts.a(p[i][df_fields_lev_1], ";", p[i][df_fields_cod_2], ";", p[i][df_fields_fo_5]);
                sa[i] = ag;
                //lv.Items[i+1].Tag = ag;
            }
            for (int i = 0; i < ip; i++)
            {
                lv.Items[i].Tag = sa[i];
            }
        }
        void setColor(Color co)
        {
            if (IsSelectNode) { treeView1.SelectedNode.ForeColor = co; }
        }
        void setModeCut(modeCut moc)
        {
            currentModeCut = moc;
            switch (moc)
            {
                case modeCut.readyToSelectSourse:
                    toolStripButton8.Enabled = false;
                    toolStripButton1.Enabled = false;
                    nodeSelect_as_sourse = treeView1.SelectedNode;
                    break;
                case modeCut.selectSourse :
                    toolStripButton8.Enabled = false;
                    toolStripButton1.Enabled = false;
                    break;
                case modeCut.selectFolder_or_Object_and_enabled_ButtonCut :
                    toolStripButton8.Enabled = true ;
                    toolStripButton1.Enabled = false;
                    break;
                case modeCut.CUT_only_on_ListView:
                    toolStripButton8.Enabled = false;
                    toolStripButton1.Enabled = false;
                    treeView1.SelectedNode.Collapse();
                    int ic = listView1.SelectedItems.Count;
                    int ii = 0;
                    ListView.SelectedListViewItemCollection si = listView1.SelectedItems;
                    //si[0].BackColor = Color.Yellow;
                    //si[1].BackColor = Color.Yellow;
                    //fs.shorts.m(fs.shorts.aa(si.Count, si[0].Text, si[1].Text));
                   // si[0].Remove(); si[1].Remove();
                    for (int j = 0; j < ic; j++)
                    {
                       // fs.shorts.m(fs.shorts.aa(si.Count, si[j].Text));
                        // fs.shorts.m(fs.shorts.aa(listView1.SelectedItems[j].Text, j, "count=", ic)); listView1.SelectedItems[j].Remove();
                        si[0].Remove();
                    }
                    break;
                case modeCut.selectFolder_adress_ready_to_Paste:
                    toolStripButton8.Enabled = false;
                    toolStripButton1.Enabled = true ;
                    nodeSelect_as_adress_for_Paste = treeView1.SelectedNode;
                    break;
                case modeCut.PASTE:
                    toolStripButton8.Enabled = false;
                    toolStripButton1.Enabled = true ;

                    /*
                    
                                        move_in_df(nodeSelect_for_cut, nodeSelect_as_adress_for_Paste);
                                        move_in_SERVER(nodeSelect_for_cut, nodeSelect_as_adress_for_Paste);
                                        move_in_tree(nodeSelect_for_cut, nodeSelect_as_adress_for_Paste);
                     * */

                    //move_arr();
                    move_arr(a_nodeSelect_for_cut, nodeSelect_as_adress_for_Paste);
                   // treeView1.CollapseAll();
                    afterExp();
                  //  treeView1.Nodes[selectSourseNodeIndex].Collapse();
                    break;
            }
        }
        void setModeMove(modeMove mom)
        {
            //currencyModeMove = mom;
            //switch (mom)
            //{
            //    case modeMove.noMove :
            //        toolStripButton8.Enabled = false;
            //        toolStripButton1.Enabled = false;
            //        listView1.Enabled = true;
            //        setColor(Color.Black);
            //        break;
            //    case modeMove.select_sourse_InTreeView:
            //        toolStripButton8.Enabled = false;
            //        toolStripButton1.Enabled = false;
            //        setColor(Color.Black); 
            //        listView1.Enabled = false;
                //    break;
                //case modeMove.clickOnFlag_ListView_enableFalse:
                //    toolStripButton8.Enabled = true;
                //    toolStripButton1.Enabled = false;
                //    listView1.Enabled = false;
                //    setColor(Color.Black);
                //    break;
                //case modeMove.select_adress_inTreeView_ready_move:
                //    toolStripButton8.Enabled = false;
                //    toolStripButton1.Enabled = true;
                //    listView1.Enabled = true;
                //    setColor(Color.Red);
            //        break;
            //}
        }
        void df_load()
        {
            if (df.Rows.Count > 0)
            {
                readTableParent_readTableChild_0_lev(treeView1, df);
            }
        }

        private void addNodes_0_lev()
        {   //df_codMAX_lev_SQL
            int ic = df_codMAX_lev(0) + 1; string so = ic.ToString();
           
            string s_FOLDER = functionService.shorts.a(FOLDER, ic), ss = functionService.nameCod.name_cod(s_FOLDER, ic); object tag = functionService.shorts.a("0;", ic, ";0");

            addToTable_AnyLevel(s_FOLDER, 0, 888);

            TreeNode tnn = new TreeNode(ss); tnn.Tag = tag;  treeView1.Nodes.Add(tnn);
        }
        private void addToTable_AnyLevel(  string sNameo, int  lev, object copo)
        {
            DataTable dto = df;
            DataRow r = dto.NewRow(); int ic = df_codMAX_lev(lev) + 1;
            r[0] = 0;
            r[1] = lev;
            r[2] = ic;
            r[3] = sNameo;
            r[4] = copo;
            r[5] = 0;
            dto.Rows.Add(r); dto.AcceptChanges();

            string sql_insert =
                string.Concat("INSERT INTO ", SCHEMA, ".", TABLENAME, "_folders(lev, cod, name, cop, fo) VALUES (", lev, ",", ic, ",'", sNameo, "',", copo, ", 0)");
            q.UpdateData(sql_insert);
        }

        private int df_codMAX_lev(int levo)
        {
           // q.sh(df);
            DataView v = new DataView(df); 
            v.RowFilter = string.Concat("lev=", levo);
            
                int ic = v.Count, im;
                
                if (ic > 0) { im = Convert.ToInt32(df.Compute("MAX(cod)", string.Concat( "cod<>888 and lev=", levo ))); return im; }
                else { return 0; }
        }
        private int df_codMAX_lev_SQL(int levo)
        {
            DataView v = new DataView(df); v.RowFilter = string.Concat("lev=", levo);
            int ic = v.Count, im;


            object oc = q.selectScalar(string.Concat("SELECT count(cod) as ccod FROM ", SCHEMA, ".", TABLENAME, "_folders_obj_TEMP WHERE cod<>888 and lev=", levo, ";"));
            object om = 0;
            if (Convert.ToInt32(oc) > 0)
            {
                om = q.selectScalar(string.Concat("SELECT MAX(cod) as maxcod FROM ", SCHEMA, ".", TABLENAME, "_folders_obj_TEMP WHERE cod<>888 and lev=", levo, ";"));
            }
            else
            {
                om = 0;
            }

            
            
            //'create table `',schemaname,'`.`',tablename,'_folders_obj_TEMP SELECT * FROM t1;' );
            //q.sh(df);
            //if (ic > 0) { im = Convert.ToInt32(df.Compute("MAX(cod)", string.Concat("cod<>888 and lev=", levo))); return im; }
            //else { return 0; }

            return Convert.ToInt32(om);
        }
        private string FOLDER
        {
            get { return q.tk(param.default_3); }
        }
        private int childeCodMax(DataTable dtco)
        {
            int im = 0; object oi = 0;
            if (dtco.Rows.Count > 0)
            {
                oi = dtco.Compute("MAX(cod)", "cod>0");
            }
            im = Convert.ToInt32(oi); return im;
        }
        private void setHTA(Hashtable htao, object levo, object codo, object foo)
        {
            htao.Clear(); htao.Add(LEV_1, levo); htao.Add(COD_2, codo); htao.Add(FO_3, foo);
        }

        private void readTableParent_readTableChild_0_lev(TreeView tv, DataTable dfo)
        {
            //q.sh(tp); q.sh(tc);

            int levo = 0;
            DataView p = new DataView(df); p.RowFilter = functionService.shorts.a("lev=", levo);
            DataView c = new DataView(df); c.RowFilter = functionService.shorts.a("lev=", levo + 1);
            string at = "";
            int ip = p.Count, ic = c.Count;

            //string 
            // setHTA(hta,0,0,0);

            TreeNode nn, nnn;

            for (int i = 0; i < ip; i++)
            {
                nn = new TreeNode(functionService.nameCod.name_cod(p[i][3], p[i][2]));
                c.RowFilter = string.Concat("cop = ", p[i][2], " and lev =", levo + 1);
                for (int j = 0; j < c.Count; j++)
                {
                    at = functionService.shorts.a(c[j][df_fields_lev_1], ";", c[j][df_fields_cod_2], ";", c[j][df_fields_fo_5]);
                    nnn = new TreeNode(functionService.nameCod.name_cod(c[j][df_fields_name_3], c[j][df_fields_cod_2])); nnn.Tag = at;
                    nn.Nodes.Add(nnn);
                }
                at = functionService.shorts.a(p[i][df_fields_lev_1], ";", p[i][df_fields_cod_2], ";", p[i][df_fields_fo_5]); nn.Tag = at;
                tv.Nodes.Add(nn);
            }
        }

        DataView df_lev(object lev)
        {
            DataView v = new DataView(df); v.RowFilter = functionService.shorts.a("lev =", lev);
            return v;
        }

        bool IsCanDelete_Node(object codo, int lev)
        {
            DataView dv = new DataView(df);
            dv.RowFilter = functionService.shorts.a("lev = ", lev + 1, " and cop = ", codo);
            int ic = dv.Count;
            if (ic > 0) { return false; }
            else
            {
                //if()
                return true;
            }
        }
        string readTAG(string tago, int indexo)
        {
            string[] sa = tago.Split(';'); return sa[indexo];
        }
        string readTAG(object tago, int indexo)
        {
            string[] sa = tago.ToString().Split(';'); return sa[indexo];
        }
        int readTAGi(object tago, int indexo)
        {
            string[] sa = tago.ToString().Split(';'); return Convert.ToInt32(sa[indexo]);
        }
        void editNode()
        {
            string atag = "";
            string sc; int lev = 0; string scc;
            WORK.FormTXT f = new WORK.FormTXT(functionService.nameCod.readName(treeView1.SelectedNode.Text));
            atag = treeView1.SelectedNode.Tag.ToString();//Convert.ToInt32(treeView1.SelectedNode.Tag);
            lev = Convert.ToInt32(readTAG(atag, iLEV_1));
            sc = readTAG(atag, iCOD_2);//q.readCod(treeView1.SelectedNode.Text);
            f.ShowDialog();
            if (f.IsEnter)
            {
                treeView1.SelectedNode.Text = functionService.nameCod.name_cod(f.txtEnter, sc);
                DataView v = df_lev(lev); string sname = f.txtEnter;
                int iv = v.Count;
                for (int i = 0; i < iv; i++)
                {
                    scc = v[i][2].ToString();
                    if (sc == scc)
                    {
                        v[i][3] = sname;
                        q.UpdateData(SQL_DML_UPDATE_node(sname, sc, lev));
                    }
                }
                df.AcceptChanges();
            }
        }
        void delNode()
        {
            string st = treeView1.SelectedNode.Text;
            string atag = treeView1.SelectedNode.Tag.ToString();
            int tag_level = Convert.ToInt32(readTAG(atag, iLEV_1)); //Convert.ToInt32(treeView1.SelectedNode.Tag);
            string sc = readTAG(atag, iCOD_2); int ic = Convert.ToInt32(sc)-10000;
            string sfo = readTAG(atag,iFO_3);
            if (sfo == "0")
            {
                if (IsCanDelete_Node(sc, tag_level))
                {
                    treeView1.SelectedNode.Remove();
                    find_delete_cod(sc, tag_level);
                }
                else { functionService.shorts.m("Группа содержит вложенные элементы."); }
            }
            else if (sfo == "1")
            {
                //fs.shorts.m(fs.shorts.aa("sfo=", sfo , " ct = ", ic));
                treeView1.SelectedNode.Remove();
               q.UpdateData( SQL_DML_UPDATE_node_MOVE(ic));
            }
        }
        private void find_delete_cod(string scod, int lev)
        {
            DataView v = new DataView(df); v.RowFilter = functionService.shorts.a("lev=", lev);
            int ic = v.Count; string s = "", sql_del = "";
            for (int i = 0; i < ic; i++)
            {
                s = v[i][2].ToString(); //fs.shorts.m(fs.shorts.aa(scod,s));
                if (scod == s) { v[i].Delete(); break; }
            }
            df.AcceptChanges();

            sql_del = SQL_DML_DELETE_node(lev, scod); q.UpdateData(sql_del);
        }
        private string SQL_DML_DELETE_node(object levo, object codo)
        {

            string s;
            s = functionService.shorts.a("DELETE FROM ", SCHEMA, ".", TABLENAME, "_folders WHERE lev = ", levo, " and cod =", codo, ";");
            return s;

        }
        private string SQL_DML_UPDATE_node(object nameo, object codo, object levo)
        {

            return functionService.shorts.a("UPDATE ", SCHEMA, ".", TABLENAME, "_folders SET name = '", nameo, "' WHERE lev = ", levo, " and cod =", codo, ";");

        }
        private string SQL_DML_UPDATE_node_MOVE(object codo)
        {

            return functionService.shorts.a("UPDATE ", SCHEMA, ".", TABLENAME, "_objects SET cop = 888 WHERE cod = ", codo, ";");

        }
        private void addNodes_if_Load_old(TreeNode tn, int levelo)
        {
            //q.sh(((DataTable)aa[levelo + 1]));
            string scodParent = functionService.nameCod.readCod(tn.Text);
            int ic = HT_level_codMAX(levelo + 1) + 1;
            string s = functionService.shorts.a(FOLDER, ic), ss = functionService.nameCod.name_cod(s, ic); //fs.shorts.m(ss);
            addToTable_AnyLevel(s, levelo + 1, scodParent);
            TreeNode tnn = new TreeNode(ss); tnn.Tag = functionService.shorts.a(levelo + 1, ";", ic, ";", 0);
            tn.Nodes.Add(tnn);
        }
        private void pasteNodes_0(TreeNode tn)
        {
            int levelo = readTAGi(tn.Tag, iLEV_1);
            //q.sh(((DataTable)aa[levelo + 1]));
            string scodParent = readTAG(tn.Tag, iCOD_2); ;// q.readCod(tn.Text);
            int ic = HT_level_codMAX(levelo + 1) + 1;
            string s = functionService.shorts.a(FOLDER, ic), ss = functionService.nameCod.name_cod(s, ic); //fs.shorts.m(ss);
            addToTable_AnyLevel(s, levelo + 1, scodParent);

            TreeNode tnn = new TreeNode(ss); tnn.Tag = functionService.shorts.a(levelo + 1, ";", ic, ";", 0);
            tn.Nodes.Add(tnn);
        }
        private void addNodes_if_Load(TreeNode tn)
        {
            int levelo = readTAGi(tn.Tag, iLEV_1);
            //q.sh(((DataTable)aa[levelo + 1]));
            string scodParent = readTAG(tn.Tag, iCOD_2); ; functionService.nameCod.readCod(tn.Text);
            int ic = HT_level_codMAX(levelo + 1) + 1;
            string s = functionService.shorts.a(FOLDER, ic), ss = functionService.nameCod.name_cod(s, ic); //functionService.shorts.m(ss);
            addToTable_AnyLevel(s, levelo + 1, scodParent);

            TreeNode tnn = new TreeNode(ss); tnn.Tag = functionService.shorts.a(levelo + 1,";",ic,";",0); //iLEV_1,iCOD_2,iFO_3 
            tn.Nodes.Add(tnn);
        }
        private int HT_level_codMAX(int levelo)
        {
            DataView p = new DataView(df); p.RowFilter = functionService.shorts.a("lev =", levelo); int ic = p.Count;

            if (ic > 0)
            {
                int im;   //dt.Rows.Count, im;
                im = Convert.ToInt32(df.Compute("MAX(cod)", functionService.shorts.a("lev=", levelo))); return im;

            }
            else
            {
                return 0;
            }

        }
        #endregion

        #region event tree
        private void listView1_Click(object sender, EventArgs e)
        {
            int ic = listView1.SelectedItems.Count;
            a_nodeSelect_for_cut = new TreeNode[ic];

            if (ic >= 1)
            {
                for (int n = 0; n < ic; n++)
                {
                    a_nodeSelect_for_cut[n] = currentItem_to_treeNode(listView1.SelectedItems[n]);
                    setModeCut(modeCut.selectFolder_or_Object_and_enabled_ButtonCut);
                }
            }
        }
        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            afterExp();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            IsSelectNode = true;

            if (currentModeCut == modeCut.CUT_only_on_ListView)
            {
                setModeCut(modeCut.selectFolder_adress_ready_to_Paste);

            }
        }

        #endregion

        #region event

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            //if (currencyModeMove == modeMove.clickOnFlag_ListView_enableFalse)
            //{
            //    setModeMove(modeMove.select_adress_inTreeView_ready_move);
            //}
            setModeCut(modeCut.CUT_only_on_ListView);
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            pastNude_inDF_inSERVER_inTreeView(treeView1.SelectedNode);
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            if (currentModeCut == modeCut.selectFolder_adress_ready_to_Paste)
            {
                setModeCut(modeCut.PASTE);
            }
        }

        private void toolStripButton6_Click_1(object sender, EventArgs e)
        {
            //if (currencyModeMove != modeMove.clickOnFlag_ListView_enableFalse)
            //{
            //    setModeMove(modeMove.clickOnFlag_ListView_enableFalse);
            //}
        }


        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            editNode();
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }
        private void FormTREELV_FormClosed(object sender, FormClosedEventArgs e)
        {
            q.con.cc();
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            addNodes_0_lev();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            addNodes_if_Load(treeView1.SelectedNode);
        }
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            delNode();
        }
        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            if (IsSelect_ForCut)
            {
                //nodeCut = nodeCurrent;
                //cutNode_tag = currentNode_tag;
                // treeView1.SelectedNode.ForeColor = Color.LightGray;
                cutNodeParent_tag = treeView1.SelectedNode.Parent.Tag.ToString();
                treeView1.SelectedNode.Remove();
            }
        }
        
        #endregion

      

        
       
        private string codParent_fromTreeView_save()
        {
                return readTAG(treeView1.SelectedNode.Parent.Tag, iCOD_2);
        }
        private TreeNode currentItem_to_treeNode(ListViewItem lvi)
        {
            TreeNode tn = new TreeNode(lvi.Text); tn.Tag = lvi.Tag;
            return tn;
        }
        private void pastNudeOld(string cutNodeTagO, string cutNudeTag_ParentO,
            string pastNodeTagO, string pasteNudeTag_ParentO)
        {
            /*
             *  cod123, lev, cod, name, cop, fo
             */

            string levCut = readTAG(cutNode_tag, iLEV_1),
                codCut = readTAG(cutNode_tag, iCOD_2),
                foCut = readTAG(cutNode_tag, iFO_3);
            string levPaste = readTAG(pastNodeTagO, iLEV_1),
                codPaste = readTAG(pastNodeTagO, iCOD_2), 
                foPaste = readTAG(pastNodeTagO, iFO_3);
            
            DataView v = new DataView(df);
            v.RowFilter = 
                string.Concat
                (
                  "lev = ", levCut,
                  " and cod = ", codCut,
                  " and fo = ", foCut
                );

            v[0]["lev"] = levPaste; 
            v[0]["fo"] = foPaste;

            string sqlUPDATE = 
                string.Concat
                (
                   "UPDATE ", SCHEMA, ".", TABLENAME, "_objects SET lev=", levPaste
                   ,", cop=0 WHERE cod = ", codCut ,";"
                );
        }

        #region cut

        private void move_arr(TreeNode[] tn_s_a, TreeNode tn_a)
        { 
            foreach (TreeNode tns in tn_s_a)
            {
                move_in_df(tns, tn_a);
                move_in_SERVER(tns, tn_a);
                move_in_tree(tns, tn_a);
            }
        }
        private void move_in_df(TreeNode tn_s, TreeNode tn_a)
        {
            // fs.shorts.m(fs.shorts.aa(tn_s.Tag, "=tn_s.Tag    tn_a.Tag=", tn_a.Tag));
            string lev_s = readTAG(tn_s.Tag, iLEV_1), cod_s = readTAG(tn_s.Tag, iCOD_2),
                fo_s = readTAG(tn_s.Tag, iFO_3);
            int lev_a = 1 + readTAGi(tn_a.Tag, iLEV_1);
            string cop_a = readTAG(tn_a.Tag, iCOD_2), fo_a = readTAG(tn_a.Tag, iFO_3);
           
            string  levs, cods, fos;

            for (int j = 0; j < df.Rows.Count; j++)
            {
                levs = df.Rows[j]["lev"].ToString();
                cods = df.Rows[j]["cod"].ToString();
                fos = df.Rows[j]["fo"].ToString();
                if (levs == lev_s && cods == cod_s && fos == fo_s)
                {
                    df.Rows[j]["lev"] = lev_a;
                    df.Rows[j]["fo"] = Convert.ToInt32(fo_s);
                    df.Rows[j]["cop"] = Convert.ToInt32(cop_a);
                }
            }
            df.AcceptChanges();
        }
        private void move_in_df_old(TreeNode tn_s, TreeNode tn_a)
        {
           // fs.shorts.m(fs.shorts.aa(tn_s.Tag, "=tn_s.Tag    tn_a.Tag=", tn_a.Tag));
            string lev_s = readTAG(tn_s.Tag, iLEV_1), cod_s = readTAG(tn_s.Tag, iCOD_2), fo_s = readTAG(tn_s.Tag, iFO_3);
            int lev_a = 1 + readTAGi(tn_a.Tag, iLEV_1);
            string cop_a = readTAG(tn_a.Tag, iCOD_2), fo_a = readTAG(tn_a.Tag, iFO_3);

            //fs.shorts.m(
            //    string.Concat
            //    (
            //    lev_s, "=lev-s  ", cod_s,"=cod-s  ", fo_s , "=fo-s  ",
            //    lev_a, "=lev-s  ", cop_a, "=cod-s  ", fo_a, "=fo-s  "
            //    )
            //    );
            
            DataView v = new DataView(df);
            v.RowFilter =
              string.Concat
                (
                  "lev = ", lev_s,
                  " and cod = ", cod_s,
                  " and fo = ", fo_s
                );
           DataTable dtt =  v.ToTable();
            
            //q.sh(dtt);
            
            //q.sh(v);
            //int i = v.Count; fs.shorts.m(i); object ov;
           // df.AcceptChanges();

            v.Sort = "lev";
            for (int j = 0; j < dtt.Rows.Count; j++) 
            { 
                //ov = v[j][0];
                v[j]["lev"] = lev_a;
                v[j]["fo"] = fo_s;
                v[j]["cop"] = cop_a;
            }


            //for (int j = 0;
            //    j < df.Select
            //    (
            //    string.Concat
            //     (
            //      "lev = ", lev_s,
            //      " and cod = ", cod_s,
            //      " and fo = ", fo_s
            //     )
            //    ); j++)
            //{ 

            //}
   

            df.AcceptChanges();
        }
        private void move_in_SERVER(TreeNode tn_s, TreeNode tn_a)
        {
            string lev_s = readTAG(tn_s.Tag, iLEV_1);
            int cod_s = readTAGi(tn_s.Tag, iCOD_2) - 10000;
            string     fo_s = readTAG(tn_s.Tag, iFO_3);
            int lev_a = 1 + readTAGi(tn_a.Tag, iLEV_1);
            string cop_a = readTAG(tn_a.Tag, iCOD_2), fo_a = readTAG(tn_a.Tag, iFO_3);

            string sqlUPDATE =
                string.Concat
                (
                   "UPDATE ", SCHEMA ,".", TABLENAME, "_objects SET lev=",
                   lev_a, 
                   ", cop=", cop_a, " WHERE cod = ",
                   cod_s, " and lev = ", lev_s, ";"
                );
           // fs.shorts.m(sqlUPDATE);
            q.UpdateData(sqlUPDATE);

        }
        private void move_in_tree(TreeNode tn_s, TreeNode tn_a)
        {
            string lev_s = readTAG(tn_s.Tag, iLEV_1), cod_s = readTAG(tn_s.Tag, iCOD_2), fo_s = readTAG(tn_s.Tag, iFO_3);
            int lev_a = 1 + readTAGi(tn_a.Tag, iLEV_1);
            string cop_a = readTAG(tn_a.Tag, iCOD_2), fo_a = readTAG(tn_a.Tag, iFO_3);

            string tagNew = string.Concat(lev_a,";", cod_s,";", fo_s) ;
            tn_s.Tag = tagNew; tn_s.ImageIndex = imageIndex( fo_s, true);
            treeView1.SelectedNode.Nodes.Add(tn_s);
        }

        #endregion



        private void pastNude_inDF_inSERVER_inTreeView(
                TreeNode tno)
        {
            ///*
            // *  cod123, lev, cod, name, cop, fo
            // */
            //// treeView1
            //// string cutNudeTag_ParentO,

            //string cutNodeTagO = cutNode_tag;
            //string pastNodeTagO = tno.Tag.ToString() ;
            //string levCut = readTAG(cutNode_tag, iLEV_1),
            //    codCut = readTAG(cutNode_tag, iCOD_2),
            //    foCut = readTAG(cutNode_tag, iFO_3);

            //int levPaste = 1 + readTAGi(pastNodeTagO, iLEV_1);
            //    //codPaste = codCut; //readTAG(pastNodeTagO, iCOD_2),
            //    //foPaste = foCut;
            //int cop = readTAGi(tno.Tag, iCOD_2);

            //DataView v = new DataView(df);
            //v.RowFilter =
            //    string.Concat
            //    (
            //      "lev = ", levCut,
            //      " and cod = ", codCut,
            //      " and fo = ", foCut
            //    );
            //v[0]["lev"] = levPaste;
            //v[0]["fo"] = foCut;
            //v[0]["cop"] = cop;

            //df.AcceptChanges();

            //string sqlUPDATE =
            //    string.Concat
            //    (
            //       "UPDATE city.listtov_objects SET lev=", 
            //       levPaste
            //       , ", cop=", cop, " WHERE cod = ",
            //       codCut, " and lev = ", levPaste, ";"
            //    );
            //q.UpdateData(sqlUPDATE);


            //TreeNode tnn = new TreeNode(ss); tnn.Tag = fs.shorts.a(levPaste, ";", codCut, ";", foCut);
            //tno.Nodes.Add(tnn);
        }

        private void move(TreeNode tn_sourse, TreeNode tn_adress)
        { 

        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {
            city7.FormKEY k = new city7.FormKEY(); //toolStripTextBox1.Text
            k.ShowDialog();
            toolStripTextBox1.Text = k.valueEnter; string filter = k.valueEnter;

            int ic = listView1.Items.Count; string[] si = new string[ic]; string[] stag = new string[ic];
            //Dictionary<string, string> di = new Dictionary<string, string>();

           // ArrayList ar = new ArrayList(); ArrayList tag = new ArrayList(); //ar.Sort();

            ListViewItem[] lva = new ListViewItem[ic]; int kk = 0;

            for (int i = 0; i < ic; i++)
            {
                si[i] = listView1.Items[i].Text;
                if (si[i].Contains(filter))
                {
                    lva[kk] = new ListViewItem(listView1.Items[i].Text);
                    lva[kk].Tag = listView1.Items[i].Tag;
                    kk++;
                }
            }
            listView1.Items.Clear(); 
            for (int j = 0; j < kk; j++)
            {
                listView1.Items.Add(lva[j]);
            }

        }

        private void toolStripButton6_Click_2(object sender, EventArgs e)
        {
            string filter = toolStripTextBox1.Text;

            int ic = listView1.Items.Count; string[] si = new string[ic]; string[] stag = new string[ic];
            //Dictionary<string, string> di = new Dictionary<string, string>();

            // ArrayList ar = new ArrayList(); ArrayList tag = new ArrayList(); //ar.Sort();

            ListViewItem[] lva = new ListViewItem[ic]; int kk = 0;

            for (int i = 0; i < ic; i++)
            {
                si[i] = listView1.Items[i].Text;
                if (si[i].Contains(filter))
                {
                    lva[kk] = new ListViewItem(listView1.Items[i].Text);
                    lva[kk].Tag = listView1.Items[i].Tag;
                    kk++;
                }
            }
            listView1.Items.Clear();
            for (int j = 0; j < kk; j++)
            {
                listView1.Items.Add(lva[j]);
            }

        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       

       
       
       
        
    }

    class VARIABLES
    {
 
    }
    class WORKFUNCTION
    { 

    }
    
}

    
    

